package com.tude._07vehicle.controller;

import com.tude._07vehicle.base.response.JsonResult;
import com.tude._07vehicle.base.utils.CacheUtils;
import com.tude._07vehicle.mapper.NoticeMapper;
import com.tude._07vehicle.pojo.vo.NoticeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "03.通知模块")
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private CacheUtils cacheUtils;

    @ApiOperation("公告展示")
    @GetMapping("list")
    public JsonResult noticeList(){
        Serializable noticeIndex = cacheUtils.get("notice_index");
        if(noticeIndex != null){
            log.debug("首页数据来自Redis数据库。。。");
            return JsonResult.ok(noticeIndex);
        }
        log.debug("首页数据来自MySQL数据库。。。");
        List<NoticeVO> noticeList = noticeMapper.noticeList();
        cacheUtils.setExpire("notice_index", (Serializable) noticeList,60, TimeUnit.SECONDS);

        return JsonResult.ok(noticeList);
    }
}
